<template>
  <div class="app-container calendar-list-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-button class="filter-item" type="primary" @click="handleCreate" icon="el-icon-plus">添加</el-button>
      <el-button class="filter-item" type="primary" @click="handleUpdate" icon="el-icon-edit">修改</el-button>
      <el-button class="filter-item" type="primary" @click="handleDel" icon="el-icon-delete">删除</el-button>
      <el-button class="filter-item" type="primary" @click="handleSync" icon="el-icon-refresh">同步</el-button>
    </div>

    <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="选择" width="65">
        <template slot-scope="scope">
          <el-radio v-model="radio"
                    :label="scope.$index"
                    @change.native="changeCheckedRadio(scope.row)">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column align="center" label="ID" prop="id"  v-if=false>
      </el-table-column>
      <el-table-column align="center" label="插件名称" prop="pluginName">
      </el-table-column>
      <el-table-column align="center" label="规则名称" prop="ruleName">
      </el-table-column>
      <el-table-column align="center" label="匹配条件" prop="matchCondition">
      </el-table-column>
      <el-table-column align="center" label="appkey" prop="appkey">
      </el-table-column>
      <el-table-column align="center" label="appsecret" prop="appsecret">
      </el-table-column>
      <el-table-column align="center" label="执行顺序" prop="ruleOrder">
      </el-table-column>
      <el-table-column align="center" label="是否开启">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabledContent"
            active-color="#13ce66"
            inactive-color="#C0C0C0"
            @change="changeSwitch(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
        <el-form-item label="插件名称" prop="pluginName">
          <el-input v-model="dataForm.pluginName" disabled></el-input>
        </el-form-item>
        <el-form-item label="规则名称" prop="ruleName">
          <el-input v-model="dataForm.ruleName"  placeholder="必填"></el-input>
        </el-form-item>
        <el-form-item label="匹配条件" prop="matchCondition">
          <el-card class="box-card">
            <el-row type="flex" align="middle" :gutter="5" prop="matchCondition_module" style="padding:5px 0;">
                <div>
                  <el-input placeholder="必填" v-model="dataForm.matchCondition.module">
                    <template slot="prepend">所属模块</template>
                  </el-input>
                </div>
            </el-row>
            <el-row type="flex" align="middle" :gutter="5" style="padding:5px 0;">
              <div>
                <el-input placeholder="必填" v-model="dataForm.matchCondition.method">
                  <template slot="prepend">调用方法</template>
                </el-input>
              </div>
            </el-row>
            <el-row type="flex" align="middle" :gutter="5" style="padding:5px 0;">
              <div>
                <el-input placeholder="必填" v-model="dataForm.matchCondition.queryType">
                  <template slot="prepend">请求类型</template>
                </el-input>
              </div>
            </el-row>
            <el-row type="flex" align="middle" :gutter="5" style="padding:5px 0;">
              <div>
                <el-input placeholder="选填" v-model="dataForm.matchCondition.ip">
                  <template slot="prepend">请求IP&nbsp;&nbsp;&nbsp;</template>
                </el-input>
              </div>
            </el-row>
          </el-card>
        </el-form-item>
        <el-form-item label="appkey" prop="appkey">
          <el-input v-model="dataForm.appKey" placeholder="必填"></el-input>
        </el-form-item>
        <el-form-item label="appSecret" prop="appSecret">
          <el-input v-model="dataForm.appSecret" placeholder="必填"></el-input>
        </el-form-item>
        <el-form-item label="执行顺序" prop="ruleOrder">
          <el-input v-model="dataForm.ruleOrder" placeholder="必填"></el-input>
        </el-form-item>
        <el-form-item label="是否开启" prop="enabled">
          <template slot-scope="scope">
            <el-switch
              v-model="enabled"
              active-color="#13ce66"
              inactive-color="#C0C0C0">
            </el-switch>
          </template>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
  import { listRule, createRule, delRule, updateRule, switchRule, syncRule} from '@/api/rule'

  export default {
    name: 'sign',
    data() {
      const validateZeroOrOne = (rule, value, callback) => {
        if (new RegExp("[01]").test(value)) {
          callback()
        } else {
          callback(new Error('只能输入0或1'))
        }
      };

      return {
        list: [],
        listLoading: true,
        radio: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        rules: {
        },
        dataForm: {
          id: '',
          pluginName: 'sign',
          ruleName: '',
          matchCondition: {
            module: '',
            method: '',
            ip: '',
            queryType: ''
          },
          appSecret: '',
          appKey: '',
          ruleOrder: '',
          enabled: ''
        },
        enabled: '',
        dialogFormVisible: false,
        dialogStatus: '',
      }

    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true;
        var requestData = {
          pluginName: 'ratelimiter'
        }
        listRule(requestData).then(response => {
          this.list = response.data.data
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.listLoading = false
        })
      },
      changeSwitch(data) {
        switchRule(data).then(response => {
          this.$notify({
            title: '成功',
            message: '操作成功',
            type: 'success',
            duration: 2000
          });
          this.getList();
        })
      },
      resetForm() {
        this.dataForm = {
          id: '',
          pluginName: 'sign',
          ruleName: '',
          matchCondition: {
            module: '',
            method: '',
            ip: '',
            queryType: ''
          },
          appSecret: '',
          appKey: '',
          ruleOrder: '',
          enabled: ''
        }
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        //enabled处理
        if(this.enabled) {
          this.dataForm.enabled = 1;
        } else {
          this.dataForm.enabled = 0;
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            createRule(this.dataForm).then(response => {
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
              this.getList();
            })
          }
        })
      },
      handleDel() {
        if(!this.dataForm.id) {
          return;
        }
        delRule(this.dataForm).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.getList()
        })
      },
      handleUpdate() {
        if(!this.dataForm.id) {
          return;
        }
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            updateRule(this.dataForm).then(() => {
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              });
              this.getList()
            })
          }
        })
      },
      changeCheckedRadio(data) {
        this.dataForm = Object.assign({}, data)
      },
      balanceAdd() {
        this.balanceData.push({});
      },
      balanceDeleteRow(index) {
        if(this.balanceData.length > 1) {
          this.balanceData.splice(index,1);
        }
      },
      handleSync() {
        var requestData = {
          pluginName: 'sign'
        }
        syncRule(requestData).then(response => {
          this.dialogFormVisible = false
          this.$notify({
            title: '成功',
            message: '同步成功',
            type: 'success',
            duration: 2000
          });
        })
      }

    }
  }
</script>

